import { Module } from 'vuex'
import { IRootState } from '@/store/types'
import { ISystemState } from './types'
import { deletePageDataById, getPageListData,createPageData,editPageData } from '@/service/main/system/system'
export const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList:[],
      menuCount:0
    }
  },
  getters: {},
  actions: {
    async getPageListAction({ commit }, payLoad: any) {
      //获取pageUrl
      const pageName = payLoad.pageName
      // const pageUrl = `${pageName}/list`
      let pageUrl = ''
      switch (pageName) {
        case 'user':
          pageUrl = '/users/list'
          break
        case 'role':
          pageUrl = '/role/list'
          break
        case 'goods':
          pageUrl = '/goods/list'
          break
        case 'menu':
          pageUrl = '/menu/list'
          break
        default:
          break
      }
      //对页面发送请求
      const pageResult = await getPageListData(pageUrl, payLoad.queryInfo)
      const { list, totalCount } = pageResult.data

      switch (pageName) {
        case 'user':
          commit('changeUsersList', list)
          commit('changeUsersCount', totalCount)
          break
        case 'role':
          commit('changeRoleList', list)
          commit('changeRoleCount', totalCount)
          break
        case 'goods':
          commit('changeGoodsList', list)
          commit('changeGoodsCount', totalCount)
          break
        case 'menu':
          commit('changeMenuList',list)
          commit('changeMenuCount',totalCount)
          break
        default:
          break
      }
    },
    async deletePageDataAction(context,payLoad:any) {
      //获取pageName和id
      const {pageName,id} = payLoad
      const pageUrl = `/${pageName}/${id}`
      //调用删除网络请求
      await deletePageDataById(pageUrl)
      //重新请求最新的数据
      context.dispatch('getPageListAction',{pageName})
    },
    async createPageDataAction(context,payLoad:any){
      //创建数据的请求
      let {pageName,newInfo} = payLoad
      console.log(pageName);

      pageName=='user'?pageName='users':pageName
      const pageUrl = `/${pageName}`
      console.log(pageUrl);
      await createPageData(pageUrl,newInfo)
      pageName=='users'?pageName='user':pageName
      context.dispatch('getPageListAction',{
        pageName,
        queryInfo:{
          offset:0,
          size:10
        }
      })
    },
    async editPageDataAction(context,payLoad:any){
      //编辑数据的请求
      let {pageName,editInfo,id} = payLoad
      if(pageName=='user'){
        pageName = 'users'
      }
      const pageUrl = `/${pageName}/${id}`
      await editPageData(pageUrl,editInfo)
      context.dispatch('getPageListAction',{
        pageName,
        queryInfo:{
          offset:0,
          size:10
        }
      })
    }
  },
  mutations: {
    changeUsersList(state, userList: any[]) {
      state.usersList = userList
    },
    changeUsersCount(state, count: number) {
      state.usersCount = count
    },
    changeRoleList(state, roleList: any[]) {
      state.roleList = roleList
      console.log(state.roleList)
    },
    changeRoleCount(state, roleCount: number) {
      state.roleCount = roleCount
    },
    changeGoodsList(state, goodsList: any[]) {
      state.goodsList = goodsList
    },
    changeGoodsCount(state, goodsCount: number) {
      state.goodsCount = goodsCount
    },
    changeMenuList(state,menuList:any[]){
      state.menuList = menuList
    },
    changeMenuCount(state,menuCount:number){
      state.menuCount = menuCount
    }
  }
}
